package com.jianganming.builder;

import java.util.Map;

/**
 * 建造者设计模式
 * 抽象建造类（Builder）：这个抽象类只是确定要生产一个产品，但是产品的名称，颜色，价格只提供类了抽象方法。
 * 具体建造类（ProductBuilder）：这里来实现抽象建造类的抽象方法。
 * 产品对象（Product）：一个属性繁多的产品
 * 指挥者类（Director）：调用建造者类的构建方法来构建产品对象。
 */
public class BuilderTest {
    public static void main(String[] args) {
        Director director = new Director(new ProductBuilder());
        Product product = director.construct();
        System.out.println(product);
    }
}

class Product{
    private String name;
    private String color;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
}

abstract class Builder{
    private Product product = new Product();

    public abstract Builder builerName(String name);

    public abstract Builder builerColor(String color);

    public abstract Builder builerPrice(double price);

    public Product getProduct() {
        return product;
    }
}

class ProductBuilder extends Builder{


    @Override
    public Builder builerName(String name) {
        System.out.println("构建手机名称："+name);
        getProduct().setName(name);
        return this;

    }

    @Override
    public Builder builerColor(String color) {
        System.out.println("构建手机颜色："+color);
        getProduct().setColor(color);
        return this;
    }

    @Override
    public Builder builerPrice(double price) {
        System.out.println("构建手机价格："+price);
        getProduct().setPrice(price);
        return this;
    }
}

class Director{
    private Builder builder;

    public Director(Builder builder){
        this.builder=builder;
    }

    public Product construct(){
        return builder.builerName("手机")
        .builerColor("红色")
        .builerPrice(8000).getProduct();
    }
}